
local function reload_map(name)

    for requires in pairs(package.loaded) do
        local info = ac.split(requires,'%.')
        if  info[1] == name then
            package.loaded[requires] = nil
            local path = "scripts\\" .. requires:gsub('%.', '\\')

            for actor in pairs(ac.allacts) do
                local src = actor._src
                if src and src:find(path) and actor.removed ~= true then
                    actor:remove()
                end
            end

            for actor in pairs(ac.message.allacts) do
                local src = actor._src
                if src and src:find(path) and actor.removed ~= true then
                    -- print('reload:',actor)
                    actor:remove()
                end
            end

            for actor in pairs(ac.hardware.allacts) do
                local src = actor._src
                if src and src:find(path) and actor.removed ~= true then
                    -- print('reload:',actor)
                    actor:remove()
                end
            end

            for actor in pairs(ac.sync.allacts) do
                local src = actor._src
                if src and src:find(path) and actor.removed ~= true then
                    -- print('reload:',actor)
                    actor:remove()
                end
            end

            for actor in pairs(ac.message.sync.allacts) do
                local src = actor._src
                if src and src:find(path) and actor.removed ~= true then
                    -- print('reload:',actor)
                    actor:remove()
                end
            end

            for actor in pairs(ac.sync_content.allacts) do
                local src = actor._src
                if src and src:find(path) and actor.removed ~= true then
                    -- print('reload:',actor)
                    actor:remove()
                end
            end

            for timer in pairs(ac.alltimers) do
                local src = timer._src
                if src and src:find(path) and timer.removed ~= true then
                    -- -- print('reload:',timer)
                    timer:remove()
                elseif timer.type == 'timerdialog' then 
                    timer:remove()
                end
            end

            local mover = require 'types.mover'
            -- for mover in pairs(mover.mover_group) do 
            --     mover:remove()
            -- end 

            for mover in pairs(ac.mover.allmovers) do 
               mover:remove()
            end 
            
            for image in pairs(ac.image.allimages) do 
                image:remove()
            end 
                        
            for _, wea in pairs(ac.weather.allweathers) do 
                wea:remove()
            end 
            
            -- for _, tag in pairs(ac.texttag2.alltexttag) do 
            --     tag:remove()
            -- end 

            for _, rgn in pairs(ac.region.allregions) do
                rgn:remove()
            end

            for _,dlg in pairs(ac.dialog.alldialogs) do 
                dlg:remove()
            end 

            for _, mb in pairs(ac.multiboard.allmultiboards) do 
                mb:remove()
            end 

            for _,unit in pairs(ac.unit.allunits) do 
                if not unit.default then
                    -- -- print('reload:',unit)
                    unit:remove()
                end
            end 

        end
    end

end 

local function reload_ui(name)

    for handle,ui in pairs(class.ui_base.allframes) do
        if ui:is_default() ~= true and ui.removed ~= true then
            ui:remove()
        end
    end
    
    local map = {
        ['slider'] = true,
        ['model'] = true,
        ['model2'] = true,
        ['highlight'] = true,
        ['button'] = true,
        ['textbutton'] = true,
        ['panel'] = true,
        ['text'] = true,
        ['simple'] = true,
        ['editbox'] = true,
        ['checkbox'] = true,
        ['ui_base'] = true,
        ['__newindex'] = true,
    }
    for name, cls in pairs(class) do 
        if map[name] == nil then 
            print('重置类', name)
            rawset(class, name, nil)
        end 
    end 

    for requires in pairs(package.loaded) do
        local info = ac.split(requires,'%.')
        if  info[1] == name then
            package.loaded[requires] = nil
            local path = "scripts\\" .. requires:gsub('%.', '\\')

            for _,frame in pairs(ac.frame.allframes) do
                local src = frame._src
                if src and src:find(path) and frame:is_default() ~= true and frame.removed ~= true then
                    -- -- print('reload:',frame , frame._src )
                    frame:remove()
                end
            end

            for actor in pairs(ac.allacts) do
                local src = actor._src
                if src and src:find(path) and actor.removed ~= true then
                    -- print('reload:',actor)
                    actor:remove()
                end
            end

            for actor in pairs(ac.message.allacts) do
                local src = actor._src
                if src and src:find(path) and actor.removed ~= true then
                    -- print('reload:',actor)
                    actor:remove()
                end
            end

            for actor in pairs(ac.hardware.allacts) do
                local src = actor._src
                if src and src:find(path) and actor.removed ~= true then
                    -- print('reload:',actor)
                    actor:remove()
                end
            end

            for actor in pairs(ac.sync.allacts) do
                local src = actor._src
                if src and src:find(path) and actor.removed ~= true then
                    -- print('reload:',actor)
                    actor:remove()
                end
            end

            for actor in pairs(ac.message.sync.allacts) do
                local src = actor._src
                if src and src:find(path) and actor.removed ~= true then
                    -- print('reload:',actor)
                    actor:remove()
                end
            end

            for timer in pairs(ac.alltimers) do
                local src = timer._src
                if src and src:find(path) and timer.removed ~= true then
                    -- print('reload:',timer)
                    timer:remove()
                end
            end

        end
    end
    
end 

local jass = require 'jass.common'

if package.local_test then
    ac.message:on "键盘按下" (function(t, code)
        
        --热更全部
        if ac.message.keyboard[code] == 'F5' then

            ac.game:notify "游戏-即将热更新"
            
            package.loaded['初始化'] = nil

            reload_ui("界面")
            --reload_map("地图")

            for _, effect in pairs(ac.effect.alleffects) do 
                if not effect.default then 
                    effect:remove()
                end 
            end 

            --for _,item in pairs(ac.item.allitems) do 
            --    if not item.default then 
            --        item:remove()
            --    end 
            --end 

            ac.game:notify "游戏-热更新"

            collectgarbage("collect")

            print('游戏-热更新')
            
            ac.wait(100,function()
                require '界面'
                --require '地图'
                
                ac.game:notify "游戏-初始化"
            end)

        end 

    end)
end 

return reload